package net.ewon.avatar.integration.agent;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import net.ewon.avatar.common.pkg.AvatarPackage;
import net.ewon.avatar.common.pkg.AvatarPackageFile;
import net.ewon.avatar.common.pkg.Version;
import net.ewon.avatar.pkgmgmt.manager.AvatarPackageManager;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.client.RestTemplate;

public class AgentMissingPackage {

	private static TestUtils testUtils;

	private static RestTemplate restTemplate;
	
	private static AvatarPackageManager avatarPackageManager;

	/**
	 * read tomcat package and write them to MonogoDB through
	 * AvatarPackageMgmtService deploy it to local via AvatarAgent
	 */
	public static void main(String[] args) throws Exception {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"classpath*:**/*/AvatarIntegrationTest-test-beans.xml");
		  
		testUtils = (TestUtils) context.getBean("testUtils");

		restTemplate = (RestTemplate) context.getBean("restTemplate");
		avatarPackageManager = (AvatarPackageManager)context.getBean("avatarPackageManager");

		AgentMissingPackage amp = new AgentMissingPackage();
		String dir = "/Users/ewon/Documents/work/apache-tomcat-7.0.30/target";
		String pkgName = "Tomcat";
		Version version = Version.parse("7.0.30");
		amp.importPackage(dir, pkgName, version);
	}

	private void importPackage(String dir, String pkgName, Version version)
			throws IOException {
		Collection<File> files = FileUtils.listFiles(new File(dir),
				TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
		
		List<String> fileNames = new ArrayList<String>();
		for (File file : files) {
			String filePath = file.getCanonicalPath();
			System.out
					.println("length="
							+ file.length()
							+ ",file="
							+ "tomcat/7.0.30/"
							+ StringUtils
									.removeStart(filePath,
											"/Users/ewon/Documents/work/apache-tomcat-7.0.30/target/Tomcat/Tomcat/"));
			AvatarPackageFile apf = new AvatarPackageFile();
			String fileName = "tomcat/7.0.30/"
					+ StringUtils
							.removeStart(filePath,
									"/Users/ewon/Documents/work/apache-tomcat-7.0.30/target/Tomcat/Tomcat/");

			apf.setName(fileName);
			apf.setData(FileUtils.readFileToByteArray(file));
			fileNames.add(fileName);

			AvatarPackageFile[] apfs = { apf };
			restTemplate
					.postForObject(
							"http://localhost:8080/AvatarPackageMgmtService/AvatarPkgMgmtService/packageFileUpload",
							apfs, String.class);
		}

		AvatarPackage ap = new AvatarPackage();
	    ap.setName(pkgName);
	    ap.setVersion(version);
	    ap.setCreatedBy("guangzh");
        ap.getFileIds().addAll(fileNames);
		restTemplate
				.postForObject(
						"http://localhost:8080/AvatarPackageMgmtService/AvatarPkgMgmtService/packageUpload",
						ap, String.class);
		
		List<AvatarPackageFile> avatarPackageFiles = getAvatarPackageFilesFromDB(ap);
		System.out.println("number of package files:" + avatarPackageFiles.size());
		

	}
	
	public List<AvatarPackageFile> getAvatarPackageFilesFromDB(AvatarPackage avatarPackage)
			throws IOException {
		List<String> fileNames = avatarPackage.getFileIds();
		return avatarPackageManager.getAvatarPackageFiles(fileNames);
	}
}
